package com.keur.money.utils.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.databinding.DialogMullaLanguageBinding
import com.keur.money.http.HttpHelper
import com.keur.money.utils.event.KeurEventType
import com.keur.money.utils.helper.SharedHelper
import com.keur.money.utils.language.LocaleContract
import com.keur.money.utils.language.MultiLanguages

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/12 17:59
 * @Annotation：语言选择 dialog
 */
class LanguageDialog : ProDialog {
    private lateinit var binding: DialogMullaLanguageBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    var isSelect = false
    //选择语言类型
    private var selectIndex = 1

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    override fun initWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.picker_view_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback) {
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogMullaLanguageBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        isSelect = false
        checkSelectStatus()

        //当前语言
        val curtLocale = MultiLanguages.getAppLanguage(context)
        when(curtLocale.language){
            LocaleContract.getSwahiliLocale().language -> {//坦桑
                selectIndex = 2
            }
            else -> {
                selectIndex = 1
            }
        }
        checkSelectLanguageStatusViews()
    }

    override fun initListener() {
        setOnClickListener(R.id.btnNext)//确认

        binding.selectLayout.setOnClickListener {
            isSelect = !isSelect
            checkSelectStatus()
        }

        binding.englishLayout.setOnClickListener {
            HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_LANGUAGE_ENGLISH, eventRemark)
            selectIndex = 1
            checkSelectLanguageStatusViews()
        }

        binding.swahiliLayout.setOnClickListener {
            HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_LANGUAGE_SWAHILI, eventRemark)
            selectIndex = 2
            checkSelectLanguageStatusViews()
        }
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        dismiss()
        if (v?.id == R.id.btnNext) {
            SharedHelper.saveKeurSPData(SharedHelper.KEUR_SPKEY.KEUR_LANGUAGE_LAUNCH,true)
            HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_LANGUAGE_NEXT, eventRemark)
            when (selectIndex) {
                1 -> {
                    showMullaEnglishClick()
                }
                else -> {
                    showMullaSwahiliClick()
                }
            }
        }
    }

    /**
     * 选择状态
     */
    private fun checkSelectStatus(){
        when(isSelect){
            true -> {
                binding.selectLayout.setBackgroundResource(R.drawable.mulla_circle_frame_ff8027ff)
                binding.imgSelect.setImageResource(R.mipmap.mulla_language_select_up)
                binding.languageLayout.visibility = View.VISIBLE
            }
            else -> {
                binding.selectLayout.setBackgroundResource(R.drawable.mulla_radius_frame_ffdddfe4_24)
                binding.imgSelect.setImageResource(R.mipmap.mulla_language_select_down)
                binding.languageLayout.visibility = View.INVISIBLE
            }
        }
    }

    /**
     * 切换选择状态
     */
    private fun checkSelectLanguageStatusViews() {
        binding.imgEnglish.setImageResource(R.mipmap.mulla_checkbox_normal)
        binding.imgSwahili.setImageResource(R.mipmap.mulla_checkbox_normal)
        when (selectIndex) {
            1 -> {
                binding.imgEnglish.setImageResource(R.mipmap.mulla_checkbox_select)
                binding.textLanguage.text = MyApp.app.getString(R.string.language_enghlish)
            }
            else -> {
                binding.imgSwahili.setImageResource(R.mipmap.mulla_checkbox_select)
                binding.textLanguage.text = MyApp.app.getString(R.string.language_swahili)
            }
        }
    }

    private fun showMullaEnglishClick() {
        mCallback?.settingLanguageEnglish()
    }

    private fun showMullaSwahiliClick() {
        mCallback?.settingLanguageSwahili()
    }

    interface IDialogCallback {
        fun settingLanguageEnglish()

        fun settingLanguageSwahili()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_LANGUAGE_SHOW, eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_LANGUAGE_DISMISS, eventRemark)
    }
}